package com.coocaa.x.app.hall.view.corner;

import android.content.Context;
import android.net.Uri;
import android.view.View;

import com.coocaa.x.app.hall.R;
import com.coocaa.x.app.hall.view.IViewComponent;
import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.uipackage.utils.CoocaaFresco;
import com.coocaa.x.uipackage.widget.CoocaaDraweeView;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

/**
 * Created by sunny on 15-6-17.
 */
public class CornerComponent implements IViewComponent {
    protected CornerView cornerView;
    protected Context mContext = null;
    protected CoocaaDraweeView mSimpleDraweeView;

    public CornerComponent(Context context) {
        this.mContext = context;
        cornerView = new CornerView(mContext);
    }

    @Override
    public void destroy() {
        cornerView.destroy();
        if (mSimpleDraweeView != null) {
            mSimpleDraweeView.destroyDrawingCache();
            mSimpleDraweeView = null;
        }
    }

    @Override
    public View getView() {
        return cornerView;
    }

    @Override
    public void setData(Object data) {

        String path = (String) data;
        LogUtils.i("sunny CornerComponent setData:" + path);

        mSimpleDraweeView = new CoocaaDraweeView(mContext);
        mSimpleDraweeView
                .getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.CENTER_INSIDE);
        mSimpleDraweeView
                .getHierarchy().setPlaceholderImage(R.drawable.bg);
        Uri uri = Uri.parse(path);
        int width = CoocaaApplication.Div(107), height = CoocaaApplication.Div(68);
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                .setResizeOptions(new ResizeOptions(width, height))
                .build();
        DraweeController controller = CoocaaFresco.newDraweeControllerBuilder(mContext)
                .setOldController(mSimpleDraweeView.getController())
                .setImageRequest(request)
                .build();
        mSimpleDraweeView.setController(controller);

        RoundingParams roundingParams = RoundingParams.fromCornersRadius(10f);
        mSimpleDraweeView.getHierarchy().setRoundingParams(roundingParams);

        cornerView.addView(mSimpleDraweeView);

    }
}
